/**
 * 请求任务队列
 */

// 加入/更新任务
export const setRequestTask = (key?: string, val?: any) => {
  if (!key) {
    return;
  }
  if (!(window as any).__REQUEST_TASKS) {
    (window as any).__REQUEST_TASKS = {
      [key]: val,
    };
    return;
  }
  (window as any).__REQUEST_TASKS[key] = val;
};

// 获取任务
export const getRequestTask = (key?: string) => {
  if (!key) {
    return undefined;
  }
  const tasks = (window as any).__REQUEST_TASKS || {};
  return tasks[key];
};

// 移除任务
export const removeRequestTask = (key?: string) => {
  if (!key) {
    return;
  }
  const tasks = (window as any).__REQUEST_TASKS || {};
  delete tasks[key];
};

// 终止http请求
export const requestAbort = (key?: string) => {
  const requestTask = getRequestTask(key);
  if (requestTask) {
    requestTask.abort();
    removeRequestTask(key);
  }
};
